package com.wuyan.locationdog.ui.device

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.textStringTrim
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DeviceListAdapter
import com.wuyan.locationdog.adapter.FenceListAdapter
import com.wuyan.locationdog.databinding.DeviceFenceListActivityBinding
import com.wuyan.locationdog.databinding.DogListActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.GroupHelp
import com.wuyan.locationdog.view.FenceAddPopup
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DogListActivity : BaseBindingActivity<DogListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { DeviceListAdapter(R.layout.item_device_list, null) }

    override fun initData() {

        setTitleText("设备列表") {

        }

        binding.apply {
            btnAdd.setOnClickListener {
                ActivityUtils.startActivity(DeviceAddActivity::class.java)
            }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无设备列表", com.arcsoft.commonbase.R.mipmap.empty_default, "添加设备后，将为您管理"))

                    setOnItemClickListener { adapter, view, position ->
                        val bundle = Bundle()
                        bundle.putSerializable("DeviceBeanContent", data[position])
                        ActivityUtils.startActivity(bundle, DeviceSetActivity::class.java)

                    }
                    addChildClickViewIds(R.id.btnEdit)
                    setOnItemChildClickListener { adapter, view, position ->

                    }
                }
            }


        }
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 9999
        map["pageNum"] = 1
        mViewModel.deviceList(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_fence_refresh, this) {
            loadNetData()
        }
        Bus.observe<String>(bus_device_info_modify, this) {
            loadNetData()
        }
        mViewModel.deviceListBean.observe(this) {
            mAdapter.setNewInstance(it.content?.toMutableList())

        }
        mViewModel.anyBean.observe(this) {
            customShowLong("删除成功")

            loadNetData()

        }
    }
}